package cn.bupt.adt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bupt.adt.dao.autogenerate.AdminUserMapper;
import cn.bupt.adt.entity.autogenerate.AdminUser;
import cn.bupt.adt.entity.autogenerate.AdminUserExample;
import cn.bupt.adt.model.AdminUserModel;
import cn.bupt.adt.service.LoginService;
import cn.bupt.adt.util.MD5Util;
@Service
public class LoginServiceImpl implements LoginService {
	@Autowired
	AdminUserMapper adminUserMapper;

	public AdminUser getUserByName(String name) {
		AdminUserExample aue = new AdminUserExample();
		aue.createCriteria().andNameEqualTo(name);
		List<AdminUser> list = adminUserMapper.selectByExample(aue);
		if(list.size() == 0)
			return null;
		return list.get(0);
	}
	
	public AdminUserModel getUserByPasswd(String passwd) {
		AdminUserExample aue = new AdminUserExample();
		aue.createCriteria().andPasswdEqualTo(MD5Util.MD5(passwd));
		List<AdminUser> list = adminUserMapper.selectByExample(aue);
		List<AdminUserModel> modelList = new ArrayList<AdminUserModel>();
			
		for(AdminUserModel adminUsermodel : modelList){ 
			AdminUser user = list.get(0);
			adminUsermodel.setId(user.getId());
			adminUsermodel.setName(user.getName());
			adminUsermodel.setPasswd(user.getPasswd());
			modelList.add(adminUsermodel);
			}
		if(modelList.size() == 0)
			return null;
		return modelList.get(0);
	}

	@Override
	public List<AdminUser> getUserByUserExample(AdminUserExample UserExample) {
		
		// TODO Auto-generated method stub
		return adminUserMapper.selectByExample(UserExample);
	}
	
	public void saveLastLoginInTme(AdminUser user) {
		user.setLastLoginInTime(new Date());
		adminUserMapper.updateByPrimaryKeySelective(user);
	}
	
	public AdminUser getUserById(Integer userId){
		return adminUserMapper.selectByPrimaryKey(userId);
		
	}

}
